package com.tpc.control.jpa;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * ReserveItem entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "reserve_item", catalog = "production_control")
public class ReserveItem implements java.io.Serializable {

	// Fields

	private ReserveItemId id;
	private ReserveHeader reserveHeader;
	private String materialNo;
	private String materialDesign;
	private String division;
	private Double reserveQty;
	private String uom;
	private Date deliveryDate;
	private Integer status;
	private Timestamp createDate;
	private String createBy;
	private Timestamp changeDate;
	private String changeBy;
	private Date confirmLabPlan;
	private Date confirmLabActual;
	private String confirmLabRemark;
	private Date summaryCostingPlan;
	private Date summaryCostingActual;
	private String summaryCostingRemark;
	private Timestamp labChangeDate;
	private String labChangeBy;
	private Date receiveRawMaterialPlan;
	private Date receiveRawMaterialActual;
	private String receiveRawMaterialRemark;
	private Timestamp rawChangeDate;
	private String rawChangeBy;
	private Date greigeStartPlan;
	private Date greigeFinishPlan;
	private String greigeFinishMachine;
	private String greigeFinishRemark;
	private Timestamp greigeChangeDate;
	private String greigeChangeBy;
	private Date dyeingStartPlan;
	private Date dyeingFinishPlan;
	private String dyeingFinishMachine;
	private String dyeingFinishRemark;
	private Timestamp dyeingChangeDate;
	private String dyeingChangeBy;
	private Date greigeStartActual;
	private Date greigeFinishActual;
	private Date dyeingStartActual;
	private Date dyeingFinishActual;
	private Set<ReserveItemSale> reserveItemSales = new HashSet<ReserveItemSale>(
			0);
	private Set<ReserveItemLabAnswer> reserveItemLabAnswers = new HashSet<ReserveItemLabAnswer>(
			0);
	private Set<ReserveItemLab> reserveItemLabs = new HashSet<ReserveItemLab>(0);
	private Set<ReserveItemGreige> reserveItemGreiges = new HashSet<ReserveItemGreige>(
			0);

	// Constructors

	/** default constructor */
	public ReserveItem() {
	}

	/** minimal constructor */
	public ReserveItem(ReserveItemId id, ReserveHeader reserveHeader,
			String materialNo, String materialDesign, String division,
			Double reserveQty, String uom, Date deliveryDate, Integer status,
			Timestamp createDate, String createBy) {
		this.id = id;
		this.reserveHeader = reserveHeader;
		this.materialNo = materialNo;
		this.materialDesign = materialDesign;
		this.division = division;
		this.reserveQty = reserveQty;
		this.uom = uom;
		this.deliveryDate = deliveryDate;
		this.status = status;
		this.createDate = createDate;
		this.createBy = createBy;
	}

	/** full constructor */
	public ReserveItem(ReserveItemId id, ReserveHeader reserveHeader,
			String materialNo, String materialDesign, String division,
			Double reserveQty, String uom, Date deliveryDate, Integer status,
			Timestamp createDate, String createBy, Timestamp changeDate,
			String changeBy, Date confirmLabPlan, Date confirmLabActual,
			String confirmLabRemark, Date summaryCostingPlan,
			Date summaryCostingActual, String summaryCostingRemark,
			Timestamp labChangeDate, String labChangeBy,
			Date receiveRawMaterialPlan, Date receiveRawMaterialActual,
			String receiveRawMaterialRemark, Timestamp rawChangeDate,
			String rawChangeBy, Date greigeStartPlan, Date greigeFinishPlan,
			String greigeFinishMachine, String greigeFinishRemark,
			Timestamp greigeChangeDate, String greigeChangeBy,
			Date dyeingStartPlan, Date dyeingFinishPlan,
			String dyeingFinishMachine, String dyeingFinishRemark,
			Timestamp dyeingChangeDate, String dyeingChangeBy,
			Date greigeStartActual, Date greigeFinishActual,
			Date dyeingStartActual, Date dyeingFinishActual,
			Set<ReserveItemSale> reserveItemSales,
			Set<ReserveItemLabAnswer> reserveItemLabAnswers,
			Set<ReserveItemLab> reserveItemLabs,
			Set<ReserveItemGreige> reserveItemGreiges) {
		this.id = id;
		this.reserveHeader = reserveHeader;
		this.materialNo = materialNo;
		this.materialDesign = materialDesign;
		this.division = division;
		this.reserveQty = reserveQty;
		this.uom = uom;
		this.deliveryDate = deliveryDate;
		this.status = status;
		this.createDate = createDate;
		this.createBy = createBy;
		this.changeDate = changeDate;
		this.changeBy = changeBy;
		this.confirmLabPlan = confirmLabPlan;
		this.confirmLabActual = confirmLabActual;
		this.confirmLabRemark = confirmLabRemark;
		this.summaryCostingPlan = summaryCostingPlan;
		this.summaryCostingActual = summaryCostingActual;
		this.summaryCostingRemark = summaryCostingRemark;
		this.labChangeDate = labChangeDate;
		this.labChangeBy = labChangeBy;
		this.receiveRawMaterialPlan = receiveRawMaterialPlan;
		this.receiveRawMaterialActual = receiveRawMaterialActual;
		this.receiveRawMaterialRemark = receiveRawMaterialRemark;
		this.rawChangeDate = rawChangeDate;
		this.rawChangeBy = rawChangeBy;
		this.greigeStartPlan = greigeStartPlan;
		this.greigeFinishPlan = greigeFinishPlan;
		this.greigeFinishMachine = greigeFinishMachine;
		this.greigeFinishRemark = greigeFinishRemark;
		this.greigeChangeDate = greigeChangeDate;
		this.greigeChangeBy = greigeChangeBy;
		this.dyeingStartPlan = dyeingStartPlan;
		this.dyeingFinishPlan = dyeingFinishPlan;
		this.dyeingFinishMachine = dyeingFinishMachine;
		this.dyeingFinishRemark = dyeingFinishRemark;
		this.dyeingChangeDate = dyeingChangeDate;
		this.dyeingChangeBy = dyeingChangeBy;
		this.greigeStartActual = greigeStartActual;
		this.greigeFinishActual = greigeFinishActual;
		this.dyeingStartActual = dyeingStartActual;
		this.dyeingFinishActual = dyeingFinishActual;
		this.reserveItemSales = reserveItemSales;
		this.reserveItemLabAnswers = reserveItemLabAnswers;
		this.reserveItemLabs = reserveItemLabs;
		this.reserveItemGreiges = reserveItemGreiges;
	}

	// Property accessors
	@EmbeddedId
	@AttributeOverrides( {
			@AttributeOverride(name = "reserveNo", column = @Column(name = "reserve_no", nullable = false, length = 20)),
			@AttributeOverride(name = "reserveLine", column = @Column(name = "reserve_line", nullable = false)) })
	public ReserveItemId getId() {
		return this.id;
	}

	public void setId(ReserveItemId id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "reserve_no", nullable = false, insertable = false, updatable = false)
	public ReserveHeader getReserveHeader() {
		return this.reserveHeader;
	}

	public void setReserveHeader(ReserveHeader reserveHeader) {
		this.reserveHeader = reserveHeader;
	}

	@Column(name = "material_no", nullable = false, length = 18)
	public String getMaterialNo() {
		return this.materialNo;
	}

	public void setMaterialNo(String materialNo) {
		this.materialNo = materialNo;
	}

	@Column(name = "material_design", nullable = false, length = 40)
	public String getMaterialDesign() {
		return this.materialDesign;
	}

	public void setMaterialDesign(String materialDesign) {
		this.materialDesign = materialDesign;
	}

	@Column(name = "division", nullable = false, length = 2)
	public String getDivision() {
		return this.division;
	}

	public void setDivision(String division) {
		this.division = division;
	}

	@Column(name = "reserve_qty", nullable = false, precision = 10)
	public Double getReserveQty() {
		return this.reserveQty;
	}

	public void setReserveQty(Double reserveQty) {
		this.reserveQty = reserveQty;
	}

	@Column(name = "uom", nullable = false, length = 6)
	public String getUom() {
		return this.uom;
	}

	public void setUom(String uom) {
		this.uom = uom;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "delivery_date", nullable = false, length = 10)
	public Date getDeliveryDate() {
		return this.deliveryDate;
	}

	public void setDeliveryDate(Date deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	@Column(name = "status", nullable = false)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Column(name = "create_date", nullable = false, length = 19)
	public Timestamp getCreateDate() {
		return this.createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	@Column(name = "create_by", nullable = false, length = 20)
	public String getCreateBy() {
		return this.createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	@Column(name = "change_date", length = 19)
	public Timestamp getChangeDate() {
		return this.changeDate;
	}

	public void setChangeDate(Timestamp changeDate) {
		this.changeDate = changeDate;
	}

	@Column(name = "change_by", length = 20)
	public String getChangeBy() {
		return this.changeBy;
	}

	public void setChangeBy(String changeBy) {
		this.changeBy = changeBy;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "confirm_lab_plan", length = 10)
	public Date getConfirmLabPlan() {
		return this.confirmLabPlan;
	}

	public void setConfirmLabPlan(Date confirmLabPlan) {
		this.confirmLabPlan = confirmLabPlan;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "confirm_lab_actual", length = 10)
	public Date getConfirmLabActual() {
		return this.confirmLabActual;
	}

	public void setConfirmLabActual(Date confirmLabActual) {
		this.confirmLabActual = confirmLabActual;
	}

	@Column(name = "confirm_lab_remark", length = 65535)
	public String getConfirmLabRemark() {
		return this.confirmLabRemark;
	}

	public void setConfirmLabRemark(String confirmLabRemark) {
		this.confirmLabRemark = confirmLabRemark;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "summary_costing_plan", length = 10)
	public Date getSummaryCostingPlan() {
		return this.summaryCostingPlan;
	}

	public void setSummaryCostingPlan(Date summaryCostingPlan) {
		this.summaryCostingPlan = summaryCostingPlan;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "summary_costing_actual", length = 10)
	public Date getSummaryCostingActual() {
		return this.summaryCostingActual;
	}

	public void setSummaryCostingActual(Date summaryCostingActual) {
		this.summaryCostingActual = summaryCostingActual;
	}

	@Column(name = "summary_costing_remark", length = 65535)
	public String getSummaryCostingRemark() {
		return this.summaryCostingRemark;
	}

	public void setSummaryCostingRemark(String summaryCostingRemark) {
		this.summaryCostingRemark = summaryCostingRemark;
	}

	@Column(name = "lab_change_date", length = 19)
	public Timestamp getLabChangeDate() {
		return this.labChangeDate;
	}

	public void setLabChangeDate(Timestamp labChangeDate) {
		this.labChangeDate = labChangeDate;
	}

	@Column(name = "lab_change_by", length = 20)
	public String getLabChangeBy() {
		return this.labChangeBy;
	}

	public void setLabChangeBy(String labChangeBy) {
		this.labChangeBy = labChangeBy;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "receive_raw_material_plan", length = 10)
	public Date getReceiveRawMaterialPlan() {
		return this.receiveRawMaterialPlan;
	}

	public void setReceiveRawMaterialPlan(Date receiveRawMaterialPlan) {
		this.receiveRawMaterialPlan = receiveRawMaterialPlan;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "receive_raw_material_actual", length = 10)
	public Date getReceiveRawMaterialActual() {
		return this.receiveRawMaterialActual;
	}

	public void setReceiveRawMaterialActual(Date receiveRawMaterialActual) {
		this.receiveRawMaterialActual = receiveRawMaterialActual;
	}

	@Column(name = "receive_raw_material_remark", length = 65535)
	public String getReceiveRawMaterialRemark() {
		return this.receiveRawMaterialRemark;
	}

	public void setReceiveRawMaterialRemark(String receiveRawMaterialRemark) {
		this.receiveRawMaterialRemark = receiveRawMaterialRemark;
	}

	@Column(name = "raw_change_date", length = 19)
	public Timestamp getRawChangeDate() {
		return this.rawChangeDate;
	}

	public void setRawChangeDate(Timestamp rawChangeDate) {
		this.rawChangeDate = rawChangeDate;
	}

	@Column(name = "raw_change_by", length = 20)
	public String getRawChangeBy() {
		return this.rawChangeBy;
	}

	public void setRawChangeBy(String rawChangeBy) {
		this.rawChangeBy = rawChangeBy;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "greige_start_plan", length = 10)
	public Date getGreigeStartPlan() {
		return this.greigeStartPlan;
	}

	public void setGreigeStartPlan(Date greigeStartPlan) {
		this.greigeStartPlan = greigeStartPlan;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "greige_finish_plan", length = 10)
	public Date getGreigeFinishPlan() {
		return this.greigeFinishPlan;
	}

	public void setGreigeFinishPlan(Date greigeFinishPlan) {
		this.greigeFinishPlan = greigeFinishPlan;
	}

	@Column(name = "greige_finish_machine", length = 20)
	public String getGreigeFinishMachine() {
		return this.greigeFinishMachine;
	}

	public void setGreigeFinishMachine(String greigeFinishMachine) {
		this.greigeFinishMachine = greigeFinishMachine;
	}

	@Column(name = "greige_finish_remark", length = 65535)
	public String getGreigeFinishRemark() {
		return this.greigeFinishRemark;
	}

	public void setGreigeFinishRemark(String greigeFinishRemark) {
		this.greigeFinishRemark = greigeFinishRemark;
	}

	@Column(name = "greige_change_date", length = 19)
	public Timestamp getGreigeChangeDate() {
		return this.greigeChangeDate;
	}

	public void setGreigeChangeDate(Timestamp greigeChangeDate) {
		this.greigeChangeDate = greigeChangeDate;
	}

	@Column(name = "greige_change_by", length = 20)
	public String getGreigeChangeBy() {
		return this.greigeChangeBy;
	}

	public void setGreigeChangeBy(String greigeChangeBy) {
		this.greigeChangeBy = greigeChangeBy;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "dyeing_start_plan", length = 10)
	public Date getDyeingStartPlan() {
		return this.dyeingStartPlan;
	}

	public void setDyeingStartPlan(Date dyeingStartPlan) {
		this.dyeingStartPlan = dyeingStartPlan;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "dyeing_finish_plan", length = 10)
	public Date getDyeingFinishPlan() {
		return this.dyeingFinishPlan;
	}

	public void setDyeingFinishPlan(Date dyeingFinishPlan) {
		this.dyeingFinishPlan = dyeingFinishPlan;
	}

	@Column(name = "dyeing_finish_machine", length = 20)
	public String getDyeingFinishMachine() {
		return this.dyeingFinishMachine;
	}

	public void setDyeingFinishMachine(String dyeingFinishMachine) {
		this.dyeingFinishMachine = dyeingFinishMachine;
	}

	@Column(name = "dyeing_finish_remark", length = 65535)
	public String getDyeingFinishRemark() {
		return this.dyeingFinishRemark;
	}

	public void setDyeingFinishRemark(String dyeingFinishRemark) {
		this.dyeingFinishRemark = dyeingFinishRemark;
	}

	@Column(name = "dyeing_change_date", length = 19)
	public Timestamp getDyeingChangeDate() {
		return this.dyeingChangeDate;
	}

	public void setDyeingChangeDate(Timestamp dyeingChangeDate) {
		this.dyeingChangeDate = dyeingChangeDate;
	}

	@Column(name = "dyeing_change_by", length = 20)
	public String getDyeingChangeBy() {
		return this.dyeingChangeBy;
	}

	public void setDyeingChangeBy(String dyeingChangeBy) {
		this.dyeingChangeBy = dyeingChangeBy;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "greige_start_actual", length = 10)
	public Date getGreigeStartActual() {
		return this.greigeStartActual;
	}

	public void setGreigeStartActual(Date greigeStartActual) {
		this.greigeStartActual = greigeStartActual;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "greige_finish_actual", length = 10)
	public Date getGreigeFinishActual() {
		return this.greigeFinishActual;
	}

	public void setGreigeFinishActual(Date greigeFinishActual) {
		this.greigeFinishActual = greigeFinishActual;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "dyeing_start_actual", length = 10)
	public Date getDyeingStartActual() {
		return this.dyeingStartActual;
	}

	public void setDyeingStartActual(Date dyeingStartActual) {
		this.dyeingStartActual = dyeingStartActual;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "dyeing_finish_actual", length = 10)
	public Date getDyeingFinishActual() {
		return this.dyeingFinishActual;
	}

	public void setDyeingFinishActual(Date dyeingFinishActual) {
		this.dyeingFinishActual = dyeingFinishActual;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "reserveItem")
	public Set<ReserveItemSale> getReserveItemSales() {
		return this.reserveItemSales;
	}

	public void setReserveItemSales(Set<ReserveItemSale> reserveItemSales) {
		this.reserveItemSales = reserveItemSales;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "reserveItem")
	public Set<ReserveItemLabAnswer> getReserveItemLabAnswers() {
		return this.reserveItemLabAnswers;
	}

	public void setReserveItemLabAnswers(
			Set<ReserveItemLabAnswer> reserveItemLabAnswers) {
		this.reserveItemLabAnswers = reserveItemLabAnswers;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "reserveItem")
	public Set<ReserveItemLab> getReserveItemLabs() {
		return this.reserveItemLabs;
	}

	public void setReserveItemLabs(Set<ReserveItemLab> reserveItemLabs) {
		this.reserveItemLabs = reserveItemLabs;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "reserveItem")
	public Set<ReserveItemGreige> getReserveItemGreiges() {
		return this.reserveItemGreiges;
	}

	public void setReserveItemGreiges(Set<ReserveItemGreige> reserveItemGreiges) {
		this.reserveItemGreiges = reserveItemGreiges;
	}
	
	/* Customize */
	private class ItemLabSortByNo implements Comparator<ReserveItemLab>{
	    public int compare(ReserveItemLab o1, ReserveItemLab o2) {
	        return o1.getId().getReserveLabNo().compareTo(o2.getId().getReserveLabNo());
	    }
	}
	
	private class ItemGreigeSortByNo implements Comparator<ReserveItemGreige>{
	    public int compare(ReserveItemGreige o1, ReserveItemGreige o2) {
	        return o1.getCommonData().getId().getProductionNo()
	        .compareTo(o2.getCommonData().getId().getProductionNo());
	    }
	}
	
	private class ItemSaleSortByNo implements Comparator<ReserveItemSale>{
	    public int compare(ReserveItemSale o1, ReserveItemSale o2) {
	        return (o1.getId().getSaleOrder()+o1.getId().getSaleItem()).compareTo((o2.getId().getSaleOrder()+o2.getId().getSaleItem()));
	    }
	}
	
	public List<ReserveItemLab> retrieveReserveItemLabsSort() {
		List<ReserveItemLab> dataList = new ArrayList<ReserveItemLab>(this.getReserveItemLabs());
		Collections.sort(dataList, new ItemLabSortByNo());
		return dataList;
	}
	
	public List<ReserveItemGreige> retrieveReserveItemGreigesSort() {
		List<ReserveItemGreige> dataList = new ArrayList<ReserveItemGreige>(this.getReserveItemGreiges());
		Collections.sort(dataList, new ItemGreigeSortByNo());
		return dataList;
	}
	
	public List<ReserveItemSale> retrieveReserveItemSalesSort() {
		List<ReserveItemSale> dataList = new ArrayList<ReserveItemSale>(this.getReserveItemSales());
		Collections.sort(dataList, new ItemSaleSortByNo());
		return dataList;
	}
	
	public List<CommonData> retrieveDyeProductions() {
		List<CommonData>  dataList = new ArrayList();
		Iterator saleItem = this.reserveItemSales.iterator();
		while (saleItem.hasNext()) {
			ReserveItemSale data = (ReserveItemSale)saleItem.next();
			if (data.getSaleData().getCommonDatas() != null) {
				List<CommonData> commonList = data.getSaleData().retrieveCommonDatasList();
				for (int i=0;i<commonList.size();i++) {
					dataList.add(commonList.get(i));
				}
			}
		}
		return dataList;
	}
	
	

}